# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=lensfun
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.3.4
pkgrel=2
pkgdesc="Database of photographic lenses and a library that allows advanced access to the database (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://lensfun.github.io/"
license=("spdx:LGPL-3.0-only AND GPL-3.0-only AND CC-BY-SA-3.0")
makedepends=("${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             # "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: for lensfun-update-data and lensfun-add-adapter")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/lensfun/lensfun/archive/v${pkgver}.tar.gz
        lensfun-update-data.patch
        cmake-mingw.patch
        use-math-defines.patch)
sha256sums=('dafb39c08ef24a0e2abd00d05d7341b1bf1f0c38bfcd5a4c69cf5f0ecb6db112'
            '8e416bf7f57cc06f765c868ce03a8fcb1a240b01f7afd972d51bba47c0d739bb'
            'e2c441b84f72b2143f649d31715f46c714a8c8a7a69a5657edeb70140fd2bef2'
            '0e2df6ad32cfac8a5c3d96155afc056046a6cb4f7ae6058678afef0051c00826')

prepare(){
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/cmake-mingw.patch"
  patch -p1 -i "${srcdir}/lensfun-update-data.patch"
  patch -p1 -i "${srcdir}/use-math-defines.patch"
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi 

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DSETUP_PY_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"Ninja" \
      "${extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DSETUP_PY_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_STATIC=OFF \
      -DBUILD_AUXFUN=ON \
      -DBUILD_LENSTOOL=ON \
      -DBUILD_TESTS=OFF \
      -DBUILD_DOC=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/lgpl-3.0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-libs"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/gpl-3.0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-apps"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/cc-by-sa-3.0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-data"
}
